<script setup>
	import { ref, reactive, computed } from "vue";
	import { getSearchAPI } from "@/api/search.js"
	import { onLoad, onUnload, onReachBottom } from "@dcloudio/uni-app";

	//搜索历史词
	const historySearch = ref(uni.getStorageSync("historySearch") || []);
	//热门搜索词
	const recommendList = ref(["美女", "帅哥", "宠物", "卡通"]);

	const isFinally = ref(false) //没有更多
	const isLoading = ref(false) // loading
	const noSearch = computed(() => !Boolean(classList.value.length)); //没有搜索结果
	//查询参数
	const queryParams = reactive({ pageNum: 1, pageSize: 12, keyword: "" })
	const classList = ref([]);

	// 重置查询参数
	const resetParams = (keyword) => {
		classList.value = []
		isFinally.value = false
		queryParams.pageNum = 1
		queryParams.pageSize = 12
		queryParams.keyword = keyword || ""
	}
	// 搜索
	const searchData = async () => {
		try {
			isLoading.value = true
			const res = await getSearchAPI(queryParams)
			classList.value.push(...res.data)
			// 是否还有数据
			if (classList.value.length < res.total) {
				queryParams.pageNum++
			} else {
				isFinally.value = true
			}
		} finally {
			isLoading.value = false
			uni.hideLoading()
		}
	}

	//点击搜索
	const onSearch = () => {
		uni.showLoading({ title: "搜索中..." })
		historySearch.value = [...new Set([queryParams.keyword, ...historySearch.value])].slice(0, 10)
		uni.setStorageSync("historySearch", historySearch.value)
		resetParams(queryParams.keyword)
		searchData()
	}

	//点击标签进行搜索
	const onClickTab = (value) => {
		resetParams()
		queryParams.keyword = value
		onSearch()
	}

	//点击清空搜索记录
	const onRemoveHistory = () => {
		uni.showModal({
			title: "是否清空历史搜索？",
			success: res => {
				if (res.confirm) {
					historySearch.value = []
					uni.removeStorageSync("historySearch")
				}
			}
		})
	}

	// 查看详情
	const onPreview = (id) => {
		// 设置缓存
		uni.setStorageSync("classifyList", classList.value)
		uni.navigateTo({ url: `/pages/preview/preview?id=${id}` })
	}

	//触底加载更多
	onReachBottom(() => {
		if (isLoading.vlaue) return
		if (isFinally.value) return
		searchData()
	})

	//关闭有页面
	onUnload(() => {
		uni.removeStorageSync("classifyList")
	})
</script>

<template>
	<view class="searchLayout">
		<!-- 搜索框 -->
		<view class="search">
			<uni-search-bar @confirm="onSearch" @cancel="resetParams('')" @clear="resetParams('')" focus placeholder="搜索"
				v-model="queryParams.keyword">
			</uni-search-bar>
		</view>
		<!-- 搜索 -->
		<view v-show="noSearch">
			<!-- 最近搜索 -->
			<view class="history" v-show="historySearch.length">
				<view class="topTitle">
					<view class="text">最近搜索</view>
					<view class="icon" @click="onRemoveHistory">
						<uni-icons type="trash" size="25"></uni-icons>
					</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in historySearch" :key="tab" @click="onClickTab(tab)">{{tab}}</view>
				</view>
			</view>
			<!-- 热门搜索 -->
			<view class="recommend">
				<view class="topTitle">
					<view class="text">热门搜索</view>
				</view>
				<view class="tabs">
					<view class="tab" v-for="tab in recommendList" :key="tab" @click="onClickTab(tab)">{{tab}}</view>
				</view>
			</view>
		</view>
		<!-- 空状态 -->
		<view class="noSearch" v-if="noSearch">
			<uv-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/search.png"></uv-empty>
		</view>
		<!-- 结果列表 -->
		<view v-else>
			<view class="list">
				<view class="item" v-for="item in classList" :key="item._id" @click="onPreview(item._id)">
					<image :src="item.smallPicurl" mode="aspectFill"></image>
				</view>
			</view>
			<!-- loading -->
			<view class="loading-box" v-show="(isLoading && classList.length) || isFinally">
				<uni-load-more :status="isFinally?'noMore':'loading'" />
			</view>
		</view>
	</view>
</template>

<style lang="scss" scoped>
	.searchLayout {
		.search {
			padding: 0 10rpx;
		}

		.topTitle {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 32rpx;
			color: #999;
		}

		.history {
			padding: 30rpx;
		}

		.recommend {
			padding: 30rpx;
		}

		.tabs {
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			padding-top: 20rpx;

			.tab {
				background: #F4F4F4;
				font-size: 28rpx;
				color: #333;
				padding: 10rpx 28rpx;
				border-radius: 50rpx;
				margin-right: 20rpx;
				margin-top: 20rpx;
			}
		}

		.list {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 5rpx;
			padding: 20rpx 5rpx;

			.item {
				height: 440rpx;

				image {
					width: 100%;
					height: 100%;
					display: block;
				}
			}
		}
	}
</style>